package com.sy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.PageBean;
import com.sy.entity.CheckHome;
import com.sy.entity.Homework;
import com.sy.entity.StudentHome;
import com.sy.entity.StudentHomeworkUpdate;
import com.sy.exception.BusinessException;
import com.sy.mapper.HomeWorkMapper;
import com.sy.mapper.StudentHomeMapper;
import com.sy.service.StudentHomeService;
import com.sy.util.AliOssCloudUtil;
import com.sy.vo.StudentHomeVo;
import com.sy.vo.StudentHomeworkUpdateVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.sy.constant.BusinessConstant.DEFAULT_CID;
import static com.sy.constant.BusinessConstant.STUDENT_HOMEWORK_STATE_YES;

/**
 * @Author:fma
 * @Date: 2021/10/12 11:18
 */
@Service
public class StuHomeServiceImpl implements StudentHomeService {
    @Autowired
    private StudentHomeMapper studentHomeMapper;

    @Autowired
    private HomeWorkMapper homeWorkMapper;

    @Override
    public List<StudentHome> getHomesState(Integer startLimit,Integer endLimit) {
        PageHelper.startPage(startLimit,endLimit);
        List<StudentHome> homesState = studentHomeMapper.getHomesState();
        if (Objects.isNull(homesState)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "得到所有的作业状态失败");
        }
        return homesState;
    }

    @Override
    public PageBean<StudentHome> getHomeStateBySno(Integer sno, Integer pageNo, Integer pageNum) {
        if (Objects.isNull(sno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        //页码不能小于1
        if (pageNo <= 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "页码必须至少为1");
        }
        PageHelper.startPage(pageNo,pageNum);
        Page<StudentHome> page = (Page<StudentHome>) studentHomeMapper.getHomeStateBySno(sno);
        PageBean<StudentHome> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        System.out.println("这是模糊查询后的总数量"+pageBean.getTotal());
        List<StudentHome> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public PageBean<StudentHome> getStatusByConditions(StudentHomeVo studentHomeVo) {
        Integer sno = studentHomeVo.getSno();
        if (Objects.isNull(sno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        String title = studentHomeVo.getTitle();
        if (StringUtils.isBlank(title) ) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业标题不能为空");
        }
        List<StudentHome> statusBySno = getStatusBySno(sno);

        boolean flag = false;
        Homework homeworkByTitle = homeWorkMapper.getHomeworkByTitle(title);
        if (!Objects.isNull(homeworkByTitle)) {
            //作业id
            Integer id = homeworkByTitle.getId();
            for (StudentHome studentHome : statusBySno) {
                Integer thehid = studentHome.gethId();
                if (thehid == id) {
                    flag = true;
                    break;
                }
            }
            studentHomeVo.setHid(id);
        }
        if (!flag) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "你没有该作业");
        }
        Integer pageNo = studentHomeVo.getPageNo();
        //页码不能小于1
        if (pageNo <= 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "页码必须至少为1");
        }
        Integer pageNum = studentHomeVo.getPageNum();
        PageHelper.startPage(pageNo,pageNum);
        Page<StudentHome> page = (Page<StudentHome>)studentHomeMapper.getStatusByConditions(studentHomeVo);
        PageBean<StudentHome> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        System.out.println("这是模糊查询后的总数量"+pageBean.getTotal());
        List<StudentHome> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public List<StudentHome> getStatusBySno(Integer sno) {
        if (Objects.isNull(sno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        List<StudentHome> statusBySno = studentHomeMapper.getStatusBySno(sno);
        if (Objects.isNull(statusBySno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "得到所有的作业状态失败");
        }
        return statusBySno;
    }


    @Override
    public PageBean<CheckHome> getHomeWorksByConditions3(Integer tno, String title, Integer pageNo, Integer pageNum) {
        //条件必须有一个
        if (tno == null) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师编号tno不能为空");
        }
        //页码不能小于1
        if (pageNo <= 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "页码必须至少为1");
        }
        PageHelper.startPage(pageNo, pageNum);
        Page<CheckHome> page = (Page<CheckHome>) studentHomeMapper.getStudentHomeWorksByConditions( tno,title );
        //将Page封装为自定义的分页返回类型
        PageBean<CheckHome> pageBean = new PageBean<>();
        //页码能超过最大值
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        System.out.println("这是模糊查询后的总数量"+pageBean.getTotal());
        List<CheckHome> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public PageBean<CheckHome> getHomeWorksByConditions4( String limit, Integer pageNo, Integer pageNum,String title) {
        //页码不能小于1
        if (pageNo <= 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "页码必须至少为1");
        }
        PageHelper.startPage(pageNo, pageNum);
        Page<CheckHome> page = (Page<CheckHome>) studentHomeMapper.getStudentHomeWorksByConditions3(limit,title);
        //将Page封装为自定义的分页返回类型
        PageBean<CheckHome> pageBean = new PageBean<>();
        //页码能超过最大值
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        System.out.println("这是模糊查询后的总数量"+pageBean.getTotal());
        List<CheckHome> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public Boolean updateStudentHomework(StudentHomeworkUpdateVo studentHomeworkUpdateVo) {
        Integer hid = studentHomeworkUpdateVo.getHid();
        Integer sno = studentHomeworkUpdateVo.getSno();
        List<String> pathName = studentHomeworkUpdateVo.getPathName();
        if (Objects.isNull(hid)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业编号不能为空");
        }
        if(Objects.isNull(sno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        if(CollectionUtils.isEmpty(pathName)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业附件未提交");
        }
        Integer integer = deleteOriginalHomework(hid, sno);
        if(integer>=0) {
           for(String myPathName:pathName){
               StudentHomeworkUpdate studentHomeworkUpdate = new StudentHomeworkUpdate();
               studentHomeworkUpdate.setHid(hid);
               studentHomeworkUpdate.setSno(sno);
               studentHomeworkUpdate.setCorrectfile(myPathName);
               studentHomeMapper.updateStudentHomework(studentHomeworkUpdate);
           }
           studentHomeMapper.changeHomeworkState(hid,sno,STUDENT_HOMEWORK_STATE_YES);
       }
        return true;
    }

    @Override
    public Integer deleteOriginalHomework(Integer hid, Integer sno) {
        AliOssCloudUtil aliOssCloudUtil = new AliOssCloudUtil();
        if (Objects.isNull(hid)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业编号不能为空");
        }
        if(Objects.isNull(sno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        List<String> allCorrectfileByHidAndSno = studentHomeMapper.getAllCorrectfileByHidAndSno(hid, sno);
        for(String url:allCorrectfileByHidAndSno){
            String[] split = url.split("/");
            String fileName=split[split.length-2]+"/"+split[split.length-1];
            aliOssCloudUtil.delete(fileName);
        }
        return  studentHomeMapper.deleteOriginalHomework(hid,sno);
    }

    @Override
    public List<CheckHome> getStudentHomeWorksByTitle(String limit, String title, Integer pageNo, Integer pageNum) {
        PageHelper.startPage(pageNo, pageNum);
        List<CheckHome> studentHomeWorksByTitle = studentHomeMapper.getStudentHomeWorksByTitle(limit, title);
        if (studentHomeWorksByTitle.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "你输入的标题名，作业不存在");
        }
        return studentHomeWorksByTitle;
    }

    @Override
    public Integer getStuHomeNum(String limit, String title) {
       return studentHomeMapper.getStudentHomeWorksByTitle(limit, title).size();

    }
}
